In [788]:
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
from mpl_toolkits.mplot3d import axes3d, Axes3D #<-- Note the capitalization!
%matplotlib inline
sns.set_style("white")
In [799]:
#functions
def feature_normalize(df_n) :
return ( ( df_n - df_n.mean() ) / df_n.std() )
def cost_function(X,y,theta):
h_theta = np.dot(theta.T,X.T).T
J = sum((h_theta - y) ** 2) / (m * 2)
return J
def gradient_function():
for n in range(iterations):
global theta
last_j[n] = cost_function(X,y,theta)
h_theta = np.dot(theta.T,X.T).T
theta = theta - (alpha / m) * np.dot((h_theta - y).T,X).T
return theta
In [800]:
df = pd.read_csv('ex1data2.txt', sep=',', header=None)
df.describe()
Out[800]:
In [801]:
df.head()
Out[801]:
In [802]:
df_norm = feature_normalize(df)
In [803]:
df_norm.insert(0,'3',np.ones(m))
df.insert(0,'3',np.ones(m))
In [825]:
df_norm.columns = np.arange(0,4)
df.columns = np.arange(0,4)
In [826]:
# initialization
theta = pd.Series(np.zeros(df_norm.columns.shape[0] - 1))
y = df_norm[3]
X = df_norm.T[0:3].T
m = df_norm.shape[0]
alpha = 0.1
iterations = 50
last_j = np.zeros(50)
In [827]:
gradient_function()
Out[827]:
In [828]:
# plotting Cost function versus Number of iterations
itr_list = np.arange(0,50)
fig = plt.figure(figsize=(12,8))
plt.plot(itr_list,last_j,'-b')
Out[828]:
In [829]:
xx = ([1650, 3] - df.mean()[1:3]) / df.std()[1:3]
xx
Out[829]:
In [830]:
xx = pd.DataFrame(xx).T
xx
Out[830]:
In [831]:
xx.insert(0,3,1)
xx
Out[831]:
In [832]:
xx.columns = np.arange(0,3)
In [835]:
np.dot(theta.T,xx.T)[0]
Out[835]:
In [848]:
y = df[3]
X = df.T[0:3].T
In [837]:
theta = np.dot(np.dot(np.dot(X.T,X),X.T),y)
In [849]:
theta
Out[849]:
In [850]:
xx = [1650, 3]
In [851]:
xx = pd.DataFrame(xx).T
In [852]:
xx.insert(0,3,1)
In [853]:
xx.columns = np.arange(0,3)
In [854]:
np.dot(theta.T,xx.T)[0]
Out[854]:
In [ ]:
In [ ]: